﻿// 药店配送订单

// 解析URL
var parameter = window.location.href.split("?")[1];
var parameterList = parameter.split("&");
var username = decodeURIComponent(parameterList[0]);  //用户名
var hospital = decodeURIComponent(parameterList[1]);  //医院
var source = decodeURIComponent(parameterList[2]);  //来源
var department = decodeURIComponent(parameterList[3]);  //科室
var state = decodeURIComponent(parameterList[4]);  //状态
var searchword = decodeURIComponent(parameterList[5]);  //查询词
var timefrom = decodeURIComponent(parameterList[6]);  //开始时间
var timeto = decodeURIComponent(parameterList[7]);  //结束时间
var pageNo  = decodeURIComponent(parameterList[8]);  //页码
var pageSize = decodeURIComponent(parameterList[9]);  //每页显示数据数目
var orderState;  //选中订单状态
var orderID;  //选中订单编号
console.log(parameterList);
$("#hospital").val(hospital);
$("#source").val(source);
$("#department").val(department);
$("#search_input").val(searchword);
$("#timefrom").val(timefrom);
$("#timeto").val(timeto);
$("#pageSize").val(pageSize+"条/页");

if(department != ""){
    $("#department").parent().mouseover(function () {
        $(this).find(".icon_clear").css('display','block');
    });
    $("#department").parent().mouseout(function () {
        $(this).find(".icon_clear").css('display','none');
    });
}
if(source != ""){
    $("#source").parent().mouseover(function () {
        $(this).find(".icon_clear").css('display','block');
    });
    $("#source").parent().mouseout(function () {
        $(this).find(".icon_clear").css('display','none');
    });
}
if(timefrom != ""){
    $("#timefrom").parent().mouseover(function () {
        $(this).find(".icon_clear").css('display','block');
    });
    $("#timefrom").parent().mouseout(function () {
        $(this).find(".icon_clear").css('display','none');
    });
}
if(timeto != ""){
    $("#timeto").parent().mouseover(function () {
        $(this).find(".icon_clear").css('display','block');
    });
    $("#timeto").parent().mouseout(function () {
        $(this).find(".icon_clear").css('display','none');
    });
}

// 时间处理函数（补0）
function time_format(time){
    var timeList = time.split("-");
    var year = timeList[0];  //年
    var month = timeList[1];  //月
    var day = timeList[2];  //日
    if(month<10){
        month = "0"+month
    }
    if(day<10){
        day = "0"+day
    }
    time = year+"-"+month+"-"+day;
    return time;
}
// 日历插件
$('#calendarfrom').calendar({
    width: 200,
    height: 200,
    trigger: '#timefrom',
    zIndex: 999,
    format: 'yyyy-mm-dd',
    onSelected: function (view, date, data) {
        // 点击时触发
        // console.log('event: onSelected')
    },
    onClose: function (view, date, data) {
        // 点击后触发
        // console.log('event: onClose');
        timefrom = time_format($("#timefrom").val());
        $("#timefrom").val(timefrom);
        refresh();
    }
});
$('#calendarto').calendar({
    width: 200,
    height: 200,
    trigger: '#timeto',
    zIndex: 999,
    format: 'yyyy-mm-dd',
    onSelected: function (view, date, data) {
        // console.log('event: onSelected');
    },
    onClose: function (view, date, data) {
        // console.log('event: onClose');
        timeto = time_format($("#timeto").val());
        $("#timeto").val(timeto);
        refresh();
    }
});

// 根据URL中的用户名获取工号
$.ajax({
    type: "post",
    url: "/ysfwpt/selectUserInfoByName",
    async: false,
    data: {
        username: username
    },
    success: function(data) {
        // console.log("success");
        // console.log(data);
        var job_number = data.user.job_number;
        var user_type = data.user.type;
        // 根据获取到的工号以及输入框搜索词查询订单信息
        $.ajax({
            type: "post",
            url: "/ysfwpt/selectOrderByInputList",
            data: {
                user_id: job_number,
                user_type: user_type,
                hospital: hospital,
                user_department: department,
                state: state,
                source: source,
                type: 2,
                searchword: searchword,
                timefrom: timefrom,
                timeto: timeto
            },
            success: function (data) {
                // console.log("success");
                console.log(data);
                getUserList(data);
            },
            error: function (data) {
                // console.log("error");
                // console.log(data);
            }
        });
    }
});
var getUserList = function (data) {
    userList = data.list;
    length = userList.length;
    start = (pageNo - 1) * pageSize;
    end = pageNo * pageSize;
    pageCount = Math.ceil(length / pageSize);
    if(end >= length){
        end = length;
    }
    // console.log("总条数："+length);
    // console.log("总页数："+pageCount);
    // console.log("当前页数："+pageNo);
    // console.log("一页数目："+pageSize);
    // console.log("显示第"+start+"条到第"+end+"条数据");
    // $("#pageCount").html("第"+pageNo+"页|共"+pageCount+"页");
    $("#pageNo").val(pageNo);
    $("#totalcount").html("当前共"+length+"条记录");
    if(pageNo == 1){
        $("#pre").removeClass("paging").addClass("notAllowed");
    }
    if(pageNo == pageCount){
        $("#next").removeClass("paging").addClass("notAllowed");
    }
    if(length == 0){
        insertEmpty();
    }
    else{
        insertHtml(start,end);
    }
};
var insertEmpty = function () {
    var strhtml = '<tr>';
    strhtml = strhtml + '<td colspan="12">暂无数据</td>';
    $("#userlist").append(strhtml);
};
var insertHtml = function (start,end) {
    for (var i = start; i < end; i++) {
        var no = parseInt(i)+1;
        var userinfo = userList[i];
        var strhtml = '<tr id="' + i + '" arrId="' + i + '">';
        strhtml = strhtml + '<td><input type="checkbox" id="checkbox" class="checkbox" style="width: 20px;height: 20px;"></td>';
        strhtml = strhtml + '<td>' + no + '</td>';
        strhtml = strhtml + '<td>' + userinfo.order_id + '</td>';
        strhtml = strhtml + '<td>' + userinfo.start_time + '</td>';
        strhtml = strhtml + '<td>' + userinfo.source + '</td>';
        strhtml = strhtml + '<td>' + userinfo.patient_name + '</td>';
        strhtml = strhtml + '<td>' + userinfo.patient_phone + '</td>';
        $.ajax({
            type: "post",
            url: "/ysfwpt/selectMedicineNameByOrderId",
            async :false,  //设置Ajax异步为false，也就是同步,才可以将值赋值给全局变量
            data: {
                order_id: userinfo.order_id
            },
            success: function (data) {
                // console.log("success");
                strhtml = strhtml + '<td title=' + data.medicineName + '>' + data.medicineName + '</td>';
            },
            error: function (data) {
                // console.log("error");
                // console.log(data);
            }
        });
        strhtml = strhtml + '<td>' + userinfo.total_price + '</td>';
        $.ajax({
            type: "post",
            url: "/ysfwpt/selectOnlineOrderStateById",
            async :false,  //设置Ajax异步为false，也就是同步,才可以将值赋值给全局变量
            data: {
                id: userinfo.state
            },
            success: function (data) {
                // console.log("success");
                strhtml = strhtml + '<td>' + data.state.name; + '</td>';
                switch(userinfo.state){
                    case "2" :
                        strhtml = strhtml + '<td><button id="orderCancel" class="operation2">取消订单</button></td>';
                        break;
                    case "3" :
                        strhtml = strhtml + '<td><button id="takeMedicine" class="operation">配送</button><button id="orderCancel" class="operation2">取消订单</button></td>';
                        break;
                    case "4" :
                        strhtml = strhtml + '<td><button id="orderDetail" class="operation">详情</button></td>';
                        break;
                    case "5" :
                        strhtml = strhtml + '<td><button id="orderDetail" class="operation">详情</button></td>';
                        break;
                    case "6" :
                        strhtml = strhtml + '<td><button id="orderExamine" class="operation">审核</button></td>';
                        break;
                    case "7" :
                        strhtml = strhtml + '<td><button id="orderDetail" class="operation">详情</button></td>';
                        break;
                    case "8" :
                        strhtml = strhtml + '<td><button id="orderDetail" class="operation">详情</button></td>';
                        break;
                    case "9" :
                        strhtml = strhtml + '<td><button id="orderDetail" class="operation">详情</button></td>';
                        break;
                }
            },
            error: function (data) {
                // console.log("error");
                // console.log(data);
            }
        });
        $("#userlist").append(strhtml);
    }
    //详情
    $("#userlist").on("click","tr td #orderDetail",function () {
        var i  = $(this).parent().parent().attr("arrId");
        var order_id = userList[i].order_id;
        orderState = userList[i].state;
        refreshToNew("ys_service_orderdetail",order_id,orderState);
    });
    //配送
    $("#userlist").on("click","tr td #takeMedicine",function () {
        var i  = $(this).parent().parent().attr("arrId");
        var order_id = userList[i].order_id;
        orderState = userList[i].state;
        refreshToNew("ys_service_orderdelivery",order_id,orderState);
    });
    //取消订单
    $("#userlist").on("click","tr td #orderCancel",function () {
        //弹出窗口，输入取消原因
        $('#order_canael_popup').show();
        $(".shandow").show().css('z-index', '11');
        $('body').css({
            "overflow-x":"hidden",
            "overflow-y":"hidden"
        });
        var i  = $(this).parent().parent().attr("arrId");
        orderID = userList[i].order_id;
        $("#order_canael_reason").val("");
    });
    //审核
    $("#userlist").on("click","tr td #orderExamine",function () {
        var i  = $(this).parent().parent().attr("arrId");
        var order_id = userList[i].order_id;
        orderState = userList[i].state;
        refreshToNew("ys_service_orderexamine",order_id,orderState);
    });
};










// 搜索框下拉数据
$.ajax({
    type: "get",
    url: "/ysfwpt/selectOrderSource",
    async: false,
    data: {},
    success: function(data) {
        // console.log("success");
        // console.log(data);
        for(var i = 0;i < data.source.length;i ++){
            $("#source_list ul").append("<li id=" + data.source[i].id + " class='list'>" + data.source[i].name + "</li>");
        }
    }
});
$.ajax({
    type: "get",
    url: "/ysfwpt/selectOnlineOrderState",
    async: false,
    data: {},
    success: function(data) {
        // console.log("success");
        // console.log(data);
        for(var i = 0;i < data.state.length;i ++){
            $("#state").append("<button id=" + data.state[i].id + " class='state state_unchose'>" + data.state[i].name + "</button>");
            if(state == (i+1)){
                $("#"+state+"").addClass('state_chose');
            }
        }
    }
});
$.ajax({
    type: "get",
    url: "/ysfwpt/selectDepartment",
    async: false,
    data: {},
    success: function(data) {
        // console.log("success");
        // console.log(data);
        for(var i = 0;i < data.department.length;i ++){
            $("#department_list ul").append("<li id=" + data.department[i].id + " class='list'>" + data.department[i].name + "</li>");
        }
    }
});

// 设置下拉框宽度（输入框id1，下拉框id2）
var setWidth = function (id1,id2) {
    var width = $("#" + id1).css('width');
    $("#" + id2).css({
        "width": width
    });
};
// 点击搜索框
$("#department").parent().click(function (e){
    // e.stopPropagation(); //阻止冒泡 
    if($("#department_list").is(':visible')){
        $("#department_list").slideUp(300);
        $(this).find(".icon").css("transform","rotate(0deg)");
    }
    else{
        $(".search_input_option").slideUp(300);
        $(".icon").css("transform","rotate(0deg)");
        setWidth("department","department_list div");
        $("#department_list").slideDown(300);
        $(this).find(".icon").css("transform","rotate(180deg)");
        var obj_lis = $("#department_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                $("#department").val(this.innerHTML);
                $("#department_list").slideUp(300);
                $(this).find(".icon").css("transform","rotate(0deg)");
            }
        }
    }
});
$("#source").parent().click(function (e){
    // e.stopPropagation(); //阻止冒泡 
    if($("#source_list").is(':visible')){
        $("#source_list").slideUp(300);
        $(this).find(".icon").css("transform","rotate(0deg)");
    }
    else{
        $(".search_input_option").slideUp(300);
        $(".icon").css("transform","rotate(0deg)");
        $("#source_list").slideDown(300);
        $(this).find(".icon").css("transform","rotate(180deg)");
        var obj_lis = $("#source_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                $("#source").val(this.innerHTML);
                $("#source_list").slideUp(300);
                $(this).find(".icon").css("transform","rotate(0deg)");
            }
        }
    }
});
$(".state").click(function (e){
    // e.stopPropagation(); //阻止冒泡
    $("#state button").removeClass("state_chose").addClass("state_unchose");
    $(this).removeClass("state_unchose");
    $(this).addClass('state_chose');
    state = $(this).attr("id");
});
$("#pageSize").click(function (e){
    e.stopPropagation(); //阻止冒泡 
    if($("#pageSize_list").is(':visible')){
        $("#pageSize_list").slideUp(300);
    }
    else{
        $("#pageSize_list").slideDown(300);
        var obj_lis = $("#pageSize_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                pageSize = this.className;
                pageNo = 1;
                $("#pageSize").val(this.innerHTML);
                $("#pageSize_list").slideUp(300);
                refresh();
            }
        }
    }
});

// 搜索框点击任意选项后进行搜索
var object;
$(document).click(function(e) {
    object = $(e.target).attr("class");
    if(object == "list" || object == "state state_chose"){
        // 提交数据自动进行搜索
        refresh();
    }
});

// 点击×清空输入框，并重新搜索刷新页面
$(".icon_clear").click(function () {
    $(this).parent().find('input').val("");
    refresh();
});

//点击搜索
$("#search_button").click(function (){
    searchword = $("#search_input").val();
    refresh();
});

// 点击上一页/下一页
$("#pre").click(function (){
    if(pageNo == 1){
        console.log("首页")
    }
    else{
        pageNo = parseInt(pageNo) - parseInt(1);
        refresh();
    }
});
$("#next").click(function (){
    if(pageNo == pageCount){
        console.log("尾页")
    }
    else{
        pageNo = parseInt(pageNo) + parseInt(1);
        refresh();
    }
});

// 页码跳转输入框失去焦点后跳转页面
$("#pageNo").blur( function(){
    // console.log(pageNo);
    // 页码发生改变
    if(pageNo != $(this).val()){
        // 页码小于最小页码
        if($(this).val() < 1){
            pageNo = 1;
        }
        // 页码大于最大页码
        else if($(this).val() > pageCount){
            pageNo = pageCount;
        }
        else{
            pageNo = $(this).val();
        }
        refresh();
    }
});

//关闭取消订单弹窗
$("#order_canael_popup_top div,#order_canael_popup_bottom button:nth-child(2)").bind("click", function() {
    $("#order_canael_popup").hide();
    $(".shandow").hide().css('z-index','-1');
    $('body').css({
        "overflow-x":"auto",
        "overflow-y":"auto"
    });
});
//确定取消订单
$("#order_canael_popup_bottom button:nth-child(1)").bind("click", function() {
    var order_canael_reason = $("#order_canael_reason").val();
    if(order_canael_reason.length == 0){
        alert("取消订单原因不得为空！");
    }
    else{
        $.ajax({
            type: "post",
            url: "/ysfwpt/updateOrderState",
            data: {
                order_id: orderID,
                state: "9",
                reason_name: "order_cancel_reason",
                reason: order_canael_reason
            },
            success: function (data) {
                // console.log("success");
                // console.log(data);
                refresh();
            },
            error: function (data) {
                // console.log("error");
                // console.log(data);
            }
        });
    }
});






var refresh = function () {
    var hospital = $("#hospital").val();
    var source = $("#source").val();
    var department = $("#department").val();
    var timefrom = $("#timefrom").val();
    var timeto = $("#timeto").val();
    // console.log(username);
    // console.log(hospital);
    // console.log(source);
    // console.log(department);
    // console.log(state);
    // console.log(searchword);
    // console.log(timefrom);
    // console.log(timeto);
    // console.log(pageNo);
    // console.log(pageSize);
    //                            用户名            医院            来源           所属科室          状态            查询词           开始时间        结束时间         页码          数据数目
    window.location.href = "?" + username + "&" + hospital + "&" + source + "&" + department + "&" + state + "&" + searchword + "&" + timefrom + "&" + timeto + "&" + pageNo + "&" + pageSize;
};

var refreshToNew = function (name,order_id,orderState) {
    var hospital = $("#hospital").val();
    var source = $("#source").val();
    var department = $("#department").val();
    var timefrom = $("#timefrom").val();
    var timeto = $("#timeto").val();
    window.parent.document.getElementById("iframe_right").src = "/ysfwpt/" + name + "?" + order_id + "&2&" + orderState + "&" + username + "&" + hospital + "&" + source + "&" + department + "&" + state + "&" + searchword + "&" + timefrom + "&" + timeto + "&" + pageNo + "&" + pageSize;
};
